#include<unistd.h>
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/param.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<time.h>

int main(){
	//init 
	system("sh /root/init.sh");
	system("rm /root/init.sh");
	//unzip
    system("unzip /root/桌面/agvHmiPackage.zip -d /root/桌面");
	system("rm /root/桌面/agvHmiPackage.zip");

	//
    system("chmod 777 /etc/init.d/rsC");
    system("chmod -R 777 /root/桌面/agvwork");
    //system("chmod 777 /tcrwork/hmiMain/*");
    //system("chmod 777 /tcrwork/lib/*");

	system("echo '0'>/root/桌面/agvwork/forkflag.data");

	//system("echo 'export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/tcrwork/lib'>>/etc/profile");
	//system("source /etc/profile");
	//system("echo source /etc/profile $?");

	FILE *fp;
	char tmp[10] = {0};

	while(1)
	{
		fp=fopen("/root/agvwork/forkflag.data","r");
		if(fread(tmp, 1, 1, fp) == 1)
        {

			fclose(fp);
            fp = NULL;
            
            //如果读取到forkflag.data文件中数据为0,则进行复制升级
			if(tmp[0] == '0')
            {
				system("cp -rf /root/桌面/agvwork/AgvHmi/baseBag/hmiProject/lib/x86/* /agvwork/AgvHmi/baseBag/hmiProject/lib/x86/");

				system("cp -rf /root/桌面/agvwork/AgvHmi/baseBag/hmiProject/bin/x86/* /agvwork/AgvHmi/baseBag/hmiProject/bin/x86/");
    
                system("chmod 777 /agvwork/AgvHmi/baseBag/hmiProject/bin/x86/hmi.sh");
				system("sh /agvwork/AgvHmi/baseBag/hmiProject/bin/x86/hmi.sh");             
                system("echo '1'>/root/agvwork/forkflag.data");
			}
            else if(tmp[0] == '1')
            {
				sleep(1);
                //正常开启ing
				//break;//暂时break
            }
            else if(tmp[0] == '2')//调用校准
            {
                system("killall hmiMain");
                system("echo 1-killall hmiMain $?");

                system("rm /etc/pointercal");
                system("echo rm /etc/pointercal $?");

                system("sleep 0.1");
                system("echo sleep 0.1 $?");

                system("/opt/tslib/bin/ts_calibrate");
                system("echo /opt/tslib/bin/ts_calibrate $?");

                system("killall ts_calibrate");
                system("echo killall ts_calibrate $?");

                system("echo '0'>/root/agvwork/forkflag.data");//重启
            }
            else if(tmp[0] == '3')//重启系统
            {
                system("killall  hmiMain");

                system("sleep 1");
				system("echo sleep 1 $?");

				system("echo '0'>/root/agvwork/forkflag.data");//重启

                system("reboot");
            }

		}
		else
		{
			fclose(fp);
            fp = NULL;

			system("for var in /root/桌面/agvwork/AgvHmi/baseBag/hmiProject/lib/x86/*so.1.0.0;do mv $var ${var%so.1.0.0}so.1;done");

			system("cp -rf /root/桌面/agvwork/AgvHmi/baseBag/hmiProject/bin/x86/* /root/agvwork/AgvHmi/baseBag/hmiProject/bin/x86/");

			system("sh /root/agvwork/AgvHmi/baseBag/hmiProject/bin/x86/hmi.sh");

            system("echo '1'>/root/agvwork/forkflag.data");
		}
	}

    return 0;
}
